package com.ctgu.controller;

import com.ctgu.service.*;
import com.ctgu.vo.Course;
import com.ctgu.vo.Score;
import com.ctgu.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Resource
    UserService userService;
    @Resource
    StudentService studentService;
    @Resource
    TeacherService teacherService;
    @Resource
    CourseService courseService;
    @Resource
    ScoreService scoreService;

    //用户列表
    @RequestMapping("/allUser")
    public String allUser(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
        List<User> tList =  userService.getAllTeacherDetail();
        List<User> sList =  userService.getAllStudentDetail();
        model.addAttribute("tList",tList);
        model.addAttribute("sList",sList);
        return "administrator/administratorUserList";
    }
    //初始化密码
    @RequestMapping("/initPassword")
    public String initPassword(HttpServletRequest request, HttpServletResponse response, String userId) throws IOException {
        int id = Integer.parseInt(userId);
        userService.initPassword(id);
        return "redirect:/admin/allUser";
    }
    //删除用户
    @RequestMapping("/deleteUser")
    public String deleteUser(HttpServletRequest request, HttpServletResponse response, String userId) throws IOException {
        int id = Integer.parseInt(userId);
        userService.deleteUser(id);
        return "redirect:/admin/allUser";
    }
    //添加用户
    @RequestMapping("/addUser")
    public String addUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Integer userId = Integer.parseInt(request.getParameter("userId"));
        String userName = request.getParameter("userName");
        Integer type = Integer.valueOf(request.getParameter("sex"));
        userService.addUser(userId,type);
        if(type == 0){
            studentService.updateName(userId, userName);
        }else if(type == 1){
            teacherService.updateName(userId, userName);
        }else{

        }
        return "redirect:/admin/allUser";
    }

    //课程列表
    @RequestMapping("/allCourser")
    public String allCourser(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
        List<Course> list =  courseService.getAllCourse();
        model.addAttribute("list",list);
        return "administrator/administratorCourseList";
    }
    //成绩列表之所有班级
    @RequestMapping("/GradeClassList")
    public String GradeClassList(Model model){
        List<Course> list =  courseService.getAllCourse();
        model.addAttribute("list",list);
        return "administrator/administratorGradeClassList";
    }
    //成绩列表
    @RequestMapping("/GradeStudentList")
    public String GradeStudentList(Model model,String courseId){
        List<Score> list = scoreService.selectStudentInClass(courseId);
        model.addAttribute("list",list);
        return "administrator/administratorGradeStudentList";
    }
    //开课申请
    @RequestMapping("/OfferClassApplication")
    public String OfferClassApplication(Model model){
        List<Course> list = courseService.offerCourse();
        model.addAttribute("list",list);
        return "administrator/administratorOfferClassApplication";
    }
    //开课详情
    @RequestMapping("/OfferClassDetail")
    public String OfferClassDetail(HttpServletRequest request, String courseName, String teacherName, String courseDetail,String courseId){
        request.getSession().setAttribute("courseName",courseName);
        request.getSession().setAttribute("teacherName",teacherName);
        request.getSession().setAttribute("courseDetail",courseDetail);
        request.getSession().setAttribute("courseId",courseId);
        return "administrator/administratorOfferClassDetail";
    }
    //拒绝开课
    @RequestMapping("/deleteClassApplication")
    public String deleteClassApplication(String courseId){
        courseService.deletecourse(courseId);
        return "redirect:/admin/OfferClassApplication";
    }
}
